package com.apchernykh.rozhlasapp.utils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

import com.apchernykh.types.ApplicationModel;

import android.util.Log;

public class ConditionalLog {

	public static boolean mIsTracingEnabled = false;
	public static com.apchernykh.player.Logger mLogger = new com.apchernykh.player.Logger();

	public static boolean isEnabled() {
		return mIsTracingEnabled;
	}

	public static void d(String iMessage) {
		d(ApplicationModel.TAG, iMessage);
	}

	public static void e(String iMessage) {
		d(ApplicationModel.TAG, iMessage);
	}

	public static void d(String iClassName, String iMessage) {
		if (!isEnabled())
			return;
		Log.d(iClassName, iMessage);
		mLogger.warn(iClassName + iMessage);
	}

	public static void e(String iClassName, String iMessage) {
		if (!isEnabled())
			return;
		Log.e(iClassName, iMessage);
		mLogger.warn(iClassName + iMessage);
	}

	public static void i(String iClassName, String iMessage) {
		if (!isEnabled())
			return;
		Log.i(iClassName, iMessage);
		mLogger.warn(iClassName + iMessage);
	}

	public static void d(String iClassName, String iMessage, Throwable e) {
		// This log is always tracingEnabled
		// if (!tracingEnabled)
		// return;
		Log.d(iClassName, iMessage, e);
		mLogger.warn(iClassName + iMessage + getStackTrace(e));
	}

	public static String getStackTrace(Throwable iThrowable) {
		final Writer aStackTraceWriter = new StringWriter();
		final PrintWriter aPrintWriter = new PrintWriter(aStackTraceWriter);
		iThrowable.printStackTrace(aPrintWriter);
		return aStackTraceWriter.toString();
	}

	public static void e(String iClassName, Throwable e) {
		e(ApplicationModel.TAG, iClassName, e);
	}

	public static void e(String iClassName, String iMessage, Throwable e) {
		// This log is always tracingEnabled
		// if (!tracingEnabled)
		// return;
		Log.e(iClassName, iMessage, e);
		mLogger.warn(iClassName + iMessage + getStackTrace(e));
	}

	public static void log(Object iCaller, String iMessage) {
		if (isEnabled()) {
			Log.e(iCaller.toString(), "Official Log String: " + iMessage);
		}
		mLogger.warn(iMessage);
	}

}
